package org.jeecg.modules.basic.entry.util;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class StringUtil {

	/**
	 * 判断字符是否是数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		return str.matches("\\d*");
	}

	/**
	 * 两个字符串相加
	 * 
	 * @return
	 */
	public static int strAddStr(String str1, String str2) {
		return StringUtil.extractNumber(str1) + StringUtil.extractNumber(str2);
	}

	/**
	 * 从字符串中提取数字
	 * 
	 * @param str
	 *            返回提取到的数字
	 * @return
	 */
	public static int extractNumber(String str) {
		if (str == null || str.trim().equals("")) {
			return 0;
		}
		String result = "";
		for (int i = 0; i < str.length(); i++) {
			if (Character.isDigit(str.charAt(i))) {
				result += str.charAt(i);
			}
		}
		if (result.trim().equals("")) {
			return 0;
		}
		return Integer.parseInt(result);
	}

	/**
	 * 获取当前年份
	 * 
	 * @return
	 */
	public static int getCurrentYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	/**
	 * 判断两个字符串相加后是否与当前的年份相等
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean strAddEqYear(Object str1, Object str2) {
		if (str1 == null || str2 == null) {
			return false;
		}
		return StringUtil.strAddStr(str1.toString(), str2.toString()) == StringUtil
				.getCurrentYear();
	}

	/**
	 * 字符串日期+天数
	 * 
	 * @param date
	 *            日期
	 * @param addCount
	 *            加的天数
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static String strDateAdd(String date, Integer addCount) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			Date d = sf.parse(date);
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(d);
			calendar.add(calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
			d = calendar.getTime(); // 这个时间就是日期往后推一天的结果
			return sf.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/*
	 * 数字不足位数左补0
	 * 
	 * @param str
	 * 
	 * @param strLength
	 */
	public static String addZeroForNum(Integer str, int strLength) {
		String result = str.toString();
		int strLen = result.length();
		if (strLen < strLength) {
			while (strLen < strLength) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(result);// 左补0
				// sb.append(str).append("0");//右补0
				result = sb.toString();
				strLen = result.length();
			}
		}
		return result;
	}
	/** 
     * 定义分割常量 （#在集合中的含义是每个元素的分割，|主要用于map类型的集合用于key与value中的分割） 
     */  
    private static final String SEP1 = ",";  
    private static final String SEP2 = "|";
    /** 
     * List转换String 
     *  
     * @param list 
     *            :需要转换的List 
     * @return String转换后的字符串 
     */  
    public static String ListToString(List<?> list) {
        StringBuffer sb = new StringBuffer();  
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == null || list.get(i) == "") {
                    continue;  
                }  
                // 如果值是list类型则调用自己  
                if (list.get(i) instanceof List) {
                    sb.append(ListToString((List<?>) list.get(i)));  
                    sb.append(SEP1);  
                } else if (list.get(i) instanceof Map) {
                    sb.append(MapToString((Map<?, ?>) list.get(i)));  
                    sb.append(SEP1);  
                } else {
                    sb.append(list.get(i));  
                    sb.append(SEP1);  
                }
            }  
        }  
        return sb.toString();  
    }  
  
    /** 
     * Map转换String 
     *  
     * @param map 
     *            :需要转换的Map 
     * @return String转换后的字符串 
     */  
    public static String MapToString(Map<?, ?> map) {  
        StringBuffer sb = new StringBuffer();  
        // 遍历map  
        for (Object obj : map.keySet()) {  
            if (obj == null) {  
                continue;  
            }  
            Object key = obj;  
            Object value = map.get(key);  
            if (value instanceof List<?>) {  
                sb.append(ListToString((List<?>) value));  
            } else if (value instanceof Map<?, ?>) {  
                sb.append(MapToString((Map<?, ?>) value));  
            } else {  
                sb.append(value.toString());  
            }  
        }  
        return sb.toString();  
    }  
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(StringUtil.strAddStr("2010", "4"));
	}



	public static String getMD5(String sourceStr) {
		String result = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(sourceStr.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			result = buf.toString();
		} catch (NoSuchAlgorithmException e) {
			System.out.println(e);
		}
		return result;
	}

	public static byte[] getBytes(File f) {
		try {
			InputStream in = new FileInputStream(f);
			ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
			byte[] b = new byte[1024];
			int n;
			while ((n = in.read(b)) != -1)
				out.write(b, 0, n);
			in.close();
			out.close();
			return out.toByteArray();
		} catch (IOException e) {
			System.out.println("***请设置文件路径***");
		}
		return null;
	}


}
